package com.rainng.coursesystem.util;

import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.regex.Pattern;

public class docx {
    private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\$\\{.*?\\}");
    private static final Pattern PLACEHOLDER_PATTERN2 = Pattern.compile("\\\\");

    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("D://docx//1G1BL52P7TR115520_220_营运货车+05-1营运货车+操纵稳定性检验记录+HWJC-549-1-20211009.docx");
        XWPFDocument document = new XWPFDocument(fis);
        fis.close();

        removePlaceholders(document);

        FileOutputStream fos = new FileOutputStream("D://outpuessssst.docx");
        document.write(fos);
        fos.close();
    }

    private static void removePlaceholders(XWPFDocument document) {
        List<XWPFTable> tables = document.getTables();
        for (XWPFTable table : tables) {
            for (XWPFTableRow row : table.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    String text = cell.getText();
                    String replacedText = PLACEHOLDER_PATTERN.matcher(text).replaceAll("");
                    String replacedText1 = PLACEHOLDER_PATTERN2.matcher(replacedText).replaceAll("");
                    cell.removeParagraph(0); // Remove all paragraphs in the cell
                    cell.setText(replacedText1); // Set the new content
                }
            }
        }
    }
}



